<template>
	<view class="preview" v-if="currentInfo">
		<swiper circular :current="currentIndex" @change="swiperChange">
			<swiper-item v-for="item,index in classList" :key="item._id"> 
				<image v-if="readImags.includes(index)" :src="item.picurl" mode="aspectFill"  @click="maskChange()"></image>
			</swiper-item>
		</swiper>
		
		<view class="mask" v-show="maskState">
			<view class="goBack" :style="{top:getStatusBarHeight() + 'px'}" @click="goBack()">
				<uni-icons type="left" size="20" color="#fff"></uni-icons>
			</view>
			<view class="count">{{currentIndex + 1}} / {{classList.length}}</view>
			<view class="time">
				<uni-dateformat :date="Date.now()" format="hh:mm"></uni-dateformat>
			</view>
			<view class="date">
				<uni-dateformat :date="Date.now()" format="MM月dd日"></uni-dateformat>
			</view>
			<view class="footer">
				<view class="box" @click="infoClick()">
					<uni-icons type="info" size="26"></uni-icons>
					<text>信息</text>
				</view>
				<view class="box" @click="scoreClick()">
					<uni-icons type="star" size="26"></uni-icons>
					<text>{{currentInfo.score}}分</text>
				</view>
				<view class="box" @click="downLoadClick()">
					<uni-icons type="download" size="26"></uni-icons>
					<text>下载</text>
				</view>
			</view>
		</view>
	</view>
	
	<uni-popup ref="infoPopup" type="bottom" :safe-area="false">
		<view class="infoPopup">
			<view class="popupHeader">
				<view></view>
				<view class="title">
					壁纸信息
				</view>
				<view class="close" @click="closeInfoClick()">
					<uni-icons type="closeempty" size="18" color="#999"></uni-icons>
				</view>
			</view>
			<scroll-view scroll-y>
				<view class="content"  v-if="currentInfo">
					<view class="row">
						<view class="label">壁纸ID:</view>
						<text selectable class="text">{{currentInfo._id}}</text>
					</view>
					
					<!-- <view class="row">
						<view class="label">分类:</view>
						<text class="text classify">明星美女</text>
					</view> -->
					
					<view class="row">
						<view class="label">发布者:</view>
						<text class="text">{{currentInfo.nickname}}</text>
					</view>
					
					<view class="row">
						<view class="label">评分</view>
						<view class="text roteBox">
							<uni-rate readonly touchable :value="currentInfo.score"/>
							<text class="score">{{currentInfo.score}}分</text>
						</view>
					</view>
					
					<view class="row">
						<view class="label">摘要:</view>
						<text selectable class="text">{{currentInfo.description}}</text>
					</view>
					
					<view class="row">
						<view class="label tabs">标签:</view>
						<text class="tab" v-for="item in currentInfo.tabs" :key="item">{{item}}</text>
					</view>
					
					<view class="copyright">
						声明:本图片来用户投稿，非商业使用，用于免费学习交流，如侵犯了您的权益，您可以拷贝壁纸ID举报至平台，因邮箱513894357@qq.com，管理将删除侵权壁纸，维护您的权益。
					</view>
					
					<view class=".safe-area-inset-bottom">
						
					</view>
				</view>
			</scroll-view>
		</view>
	</uni-popup>
	
	<uni-popup ref="scorePopup" :is-mask-click="false">
		<view class="scorePopup">
			<view class="popupHeader">
				<view></view>
				<view class="title">
					{{isScore ? "已评分~" : "壁纸评分"}}
				</view>
				<view class="close" @click="closeScoreClick()">
					<uni-icons type="closeempty" size="18" color="#999"></uni-icons>
				</view>
			</view>
			<view class="content">
				<uni-rate v-model="scoreUesr" allowHalf :disabled="isScore" disabled-color="#ffca3e"></uni-rate>
				<text class="text">{{scoreUesr}}分</text>
			</view>
			<view class="footer">
				<button type="default" size="mini" plain :disabled="!scoreUesr || isScore" @click="submitScore()" >确认评分</button>
			</view>
		</view>
	</uni-popup>
</template>

<script setup>
import { ref } from 'vue';
import {onLoad, onShareAppMessage, onShareTimeline} from "@dcloudio/uni-app"
import {getStatusBarHeight} from '@/utils/system.js'
import {apiGetSetupScore, apiDownLoadWall, apiDetailWall} from "@/apis/apis.js"
import { type } from 'os';

const maskState = ref(true);
const infoPopup = ref(null);
const scorePopup = ref(null);
const scoreUesr = ref(0);
const classList = ref([]);
const currentIndex = ref(0);
const readImags = ref([]);
const currentInfo = ref(null);
const isScore = ref(false);
const currentId = ref(0)

//分享给朋友
onShareAppMessage(()=>{
	return {
		title:"卷毛壁纸",
		path:"/pages/preview/preview?id=" + currentId.value + "&type=share",
		imageUrl:"/static/images/logo.png"
	}
})

//分享朋友圈
onShareTimeline(()=>{
	return {
		title:"卷毛壁纸",
		query:"id=" + currentId.value + "&type=share"
	}
})

//下载壁纸到相册
const downLoadClick = async ()=>{
	// #ifdef H5
	
	uni.showModal({
		title:"请长按保存~",
		showCancel:false
	})
	// #endif
	
	// #ifndef H5
	
	try{
		uni.showLoading({
			title:"下载中...",
			mask:true
		})
		
		//壁纸下载接口
		let {classid,_id:wallId} = currentInfo.value;
		let res = await apiDownLoadWall({
			classid,
			wallId
		})
		// console.log(res);
		if(res.errCode != 0) throw res;
		
		uni.getImageInfo({
			src:currentInfo.value.picurl,
			success:(res) => {
				// console.log(res);
				uni.saveImageToPhotosAlbum({  
					filePath:res.path,
					success:(res) => {
						// console.log(res);
					},
					fail:(err) => {
						if(err.errMsg == "saveImageToPhotosAlbum:fail cancel"){
							uni.showToast({
								title:"保存失败.请重新点击下载",
								icon:"none"
							})
							return
						}
						// console.log(err);
						
						uni.showModal({
							title:"授权提示",
							content:"需要授权保存相册",
							success: (res) => {
								if(res.confirm){
									uni.openSetting({
										success: (setting) => {
											// console.log(setting);
											if(setting.authSetting["scope.writePhotosAlbum"]){
												uni.showToast({
													title:"获取授权成功",
													icon:"none"
												}),
												uni.getImageInfo({
													src:currentInfo.value.picurl,
													success:(res) => {
														uni.saveImageToPhotosAlbum({
															filePath:res.path,
														})
													}
												})
											}else{
												uni.showToast({
													title:"获取授权失败",
													icon:"none"
												})
											}
										}
									})
								}
								
							}
						})
					},
					complete: () => {
						uni.hideLoading()
					}
				})
			}
		})
	}catch(err){
		// console.log(err);
		uni.hideLoading()
	}
	
	
	// #endif
}

//swiper中的图片懒加载
const readImagesFun = ()=>{
	readImags.value.push(
		currentIndex.value<=0 ? classList.value.length-1 : currentIndex.value-1,
		currentIndex.value,
		currentIndex.value>=classList.value.length-1 ? 0 : currentIndex.value+1
	)
	readImags.value = [...new Set(readImags.value)]
}

//swiper中的current改变时触发
const swiperChange = (e)=>{
	console.log(classList.value[currentIndex.value]);
	currentIndex.value = e.detail.current
	
	//获取每一张照片的信息
	currentInfo.value = classList.value[currentIndex.value]
	
	readImagesFun()
	
	// console.log(currentInfo.value);
}

//获取照片的index
onLoad(async (e)=>{
	currentId.value = e.id
	if(e.type == 'share'){
		let res = await apiDetailWall({id:currentId.value})
		classList.value = res.data
	}
	currentIndex.value = classList.value.findIndex(item=>item._id == e.id)
	
	//获取每一张照片的信息
	currentInfo.value = classList.value[currentIndex.value]
	// console.log(currentInfo.value);
	
	readImagesFun()
	// console.log(currentIndex.value);
	// console.log(readImags.value);
})

//获取缓存数据
const storeWallList = uni.getStorageSync("storeWallList") || []  //缓存没有数据使用map会报错

//处理数据中的后缀名
classList.value = storeWallList.map(item=>{
	return {
		...item,
		picurl:item.smallPicurl.replace("_small.webp",".jpg")
	}
})

//返回上一页
const goBack = ()=>{
	uni.navigateBack()
}

//确认评分
const submitScore = async ()=>{
	uni.showLoading({
		title:"加载中..."
	})
	let {classid,_id:wallId} = currentInfo.value
	
	let res = await apiGetSetupScore({
		classid,
		wallId,
		userScore:scoreUesr.value
	})
	if(res.errCode === 0){
		uni.showToast({
			title:"评分成功",
			icon:"none"
		})
		
		
		classList.value[currentIndex.value].userScore = scoreUesr.value
		uni.setStorageSync("storeWallList",classList.value)
		closeScoreClick()
	}
	
	uni.hideLoading()
	
	// console.log(res);
	// console.log(currentInfo);
}

//关闭评分弹窗
const closeScoreClick = ()=>{
	scorePopup.value.close();
	scoreUesr.value = 0;
	isScore.value = false;
}
//评分弹窗
const scoreClick = ()=>{
	if(currentInfo.value.userScore){
		isScore.value = true;
		scoreUesr.value = currentInfo.value.userScore
	}
	scorePopup.value.open();
}

//关闭信息弹窗
const closeInfoClick = ()=>{
	infoPopup.value.close();
}
//信息弹窗
const infoClick = ()=>{
	infoPopup.value.open();
}

//遮罩层状态
const maskChange = ()=>{
	maskState.value = !maskState.value
}

</script>

<style lang="scss" scoped>
.preview{
	width: 100vw;
	height: 100vh;
	position: relative;
	swiper{
		width: 100%;
		height: 100%;
		swiper-item{
			width: 100%;
			height: 100%;
			image{
				width: 100%;
				height: 100%;
			}
		}
	}
	.mask{
		&>view{
			position: absolute;
			left: 50%;
			transform: translatex(-50%);
			color: $uni-color-text4;
			width: fit-content;
		}
		.goBack{
			width: 38px;
			height: 38px;
			position: absolute;
			top: 0;
			left: 10vw;
			border: 1rpx solid $uni-color-text4;
			border-radius: 50%;
			backdrop-filter: blur(10rpx);
			display: flex;
			align-items: center;
			justify-content: center;
		}
		.count{
			top: 10vh;
			background-color: rgba(0, 0, 0, 0.3);
			backdrop-filter: blur(10rpx);
			padding: 2rpx 28rpx;
			border-radius: 50rpx;
			font-size: 26rpx;
		}
		.time{
			top: calc(10vh + 80rpx);
			font-size: 130rpx;
			font-weight: 350;
			text-shadow: 0 4rpx rgba(0, 0, 0, 0.3);
			line-height: 1em;
		}
		.date{
			top: calc(10vh + 230rpx);
			font-size: 32rpx;
			text-shadow: 0 2rpx rgba(0, 0, 0, 0.3);
		}
		.footer{
			width: 490rpx;
			height: 120rpx;
			bottom: 10vh;
			display: flex;
			justify-content: space-between;
			align-items: center;
			background-color: rgba(255,255,255,0.8);
			padding: 10rpx 50rpx;
			border-radius: 60rpx;
			box-shadow: 0 2rpx 0 rgba(0, 0, 0, 0.1);
			backdrop-filter: blur(20rpx);
			.box{
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				padding: 2rpx 12rpx;
				.uni-icons{
					padding-bottom: 10rpx;
				}
				text{
					color: $uni-color-text2;
					font-size: 26rpx;
				}
			}
		}
	}
}

.infoPopup{
	background-color: #fff;
	padding: 30rpx;
	border-radius: 30rpx 30rpx 0 0;
	overflow: hidden;
	.popupHeader{
		display: flex;
		align-items: center;
		justify-content: space-between;
		.title{
			color: $uni-color-text2;
			font-size: 26rpx;
		}
		.close{
			padding: 6rpx;
		}
	}
	scroll-view{
		max-height: 50vh;
		.content{
			.row{
				display: flex;
				padding: 16rpx 0;
				font-size: 32rpx;
				line-height: 1.7em;
				.label{
					color: $uni-color-text3;
					width: 140rpx;
					text-align: right;
					font-size: 30rpx;
				}
				.text{
					flex: 1;
					width: 0;
					padding-left: 20rpx;
				}
				.roteBox{
					display: flex;
					align-items: center;
					.score{
						font-size: 26rpx;
						color: $uni-color-text2;
						padding-left: 10rpx;
					}
				}
				.tabs{
					display: flex;
					flex-wrap: wrap;
				}
				.tab{
					border: 1px solid $uni-color-brand;
					color: $uni-color-brand;
					font-size: 22rpx;
					padding: 10rpx 30rpx;
					border-radius: 40rpx;
					line-height: 1em;
					margin: 0 10rpx 10rpx 0;
				}
				.classify{
					color: $uni-color-brand;
				}
			}
			.copyright{
				font-size: 28rpx;
				padding: 20rpx;
				background-color: #f6f6f6;
				color: #666;
				border-radius: 10rpx;
				margin: 20rpx 0;
				line-height: 1.6em;
			}
		}
	}
}

.scorePopup{
	background-color: #fff;
	padding: 30rpx;
	width: 70vw;
	border-radius: 30rpx;
	.popupHeader{
		display: flex;
		align-items: center;
		justify-content: space-between;
		.title{
			color: $uni-color-text2;
			font-size: 26rpx;
		}
		.close{
			padding: 6rpx;
		}
	}
	.content{
		padding: 30rpx 0;
		display: flex;
		justify-content: center;
		align-items: center;
		.text{
			color: #FFCA3E;
			width: 80rpx;
			line-height: 1em;
			text-align: right;
		}
	}
	.footer{
		display: flex;
		
	}
}

</style>
